/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.scriptmark.core.dispose;

import com.jspx.scriptmark.Phrase;
import com.jspx.scriptmark.ScriptRunner;
import com.jspx.scriptmark.core.TagNode;
import com.jspx.scriptmark.core.EnvRunner;
import com.jspx.scriptmark.core.block.IfBlock;
import com.jspx.scriptmark.core.block.ElseBlock;
import com.jspx.scriptmark.exception.ScriptRunException;
import com.jspx.utils.ObjectUtil;
import com.jspx.utils.StringUtil;
import java.util.List;
import java.io.Writer;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2009-1-2
 * Time: 16:08:00
 */
public class IfPhrase implements Phrase {

    private static final long serialVersionUID = -171223113502426477L;

    public int getRun(EnvRunner env, TagNode tagNode, Writer out) throws Exception {
        ScriptRunner scriptEngine = env.getScriptRunner();
        IfBlock ifBlock = (IfBlock) tagNode;

        String w = ifBlock.getWhere();
        boolean bw =( "!".equals(StringUtil.trim(w)) || (!StringUtil.isNULL(w) && ObjectUtil.toBoolean(scriptEngine.eval(w,ifBlock.getLineNumber()))));
        if (bw)
        {
            ////if true begin
            List<TagNode> ifList = ifBlock.getTrueNode();
            int bc;
            for (TagNode ifn : ifList) {
                bc = env.runBlock(ifn, out);
                if (bc!=0) return bc;
            }
            ifList.clear();
            ////if true end
        } else {
            ///////if false to do else begin
            List<TagNode>  elseList = ifBlock.getElseBlock();
            ElseBlock defaultBlock = null;
            boolean isRun = false;
            if (elseList != null) {
                for (TagNode elNode : elseList) {
                    ElseBlock elseBlock = (ElseBlock) elNode;
                    w = elseBlock.getWhere();
                    if (w == null) {
                        if (defaultBlock != null) {
                            throw new ScriptRunException(tagNode,"if two else default");
                        }
                        defaultBlock = elseBlock;
                        continue;
                    }
                    try {
                        bw = com.jspx.utils.ObjectUtil.toBoolean(scriptEngine.eval(w,ifBlock.getLineNumber()));
                    } catch (Exception e) {
                        throw new ScriptRunException(tagNode,"if two else default");
                    }
                    if (bw) {
                        isRun = true;
                        List<TagNode> elseNode = elseBlock.getChildList();
                        if (elseNode != null) {
                            int bc;
                            for (TagNode eNode : elseNode) {
                                try{
                                    bc = env.runBlock(eNode, out);
                                } catch (Exception e) {
                                    throw new ScriptRunException(eNode,tagNode.getTagName());
                                }
                                if (bc!=0) return bc;
                            }
                        }
                        break;
                    }
                }
            }
            if (!isRun && defaultBlock != null) {
                env.runBlock(defaultBlock, out);
            }
            ///////if false to do else end
        }
        return 0;
    }
}